package com.baihui.crm.util;

import com.baihui.taobao.db.UserDAO;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created by Kevin.
 * User: HSZM1106-0312
 * Date: 12-1-27
 * Time: 下午3:28
 */
public class ExportUtil {

    /**
     * 获取星座
     *
     * @param birthday
     * @return
     */
    public static String getConstellation(Timestamp birthday) {
        if (birthday == null) return "";
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        String tmp = dateFormat.format(birthday);
        if ("03-21".compareTo(tmp) == -1 && "04-20".compareTo(tmp) == 1) {
            return "白羊座";
        } else if ("04-21".compareTo(tmp) == -1 && "05-21".compareTo(tmp) == 1) {
            return "金牛座";
        } else if ("05-22".compareTo(tmp) == -1 && "06-21".compareTo(tmp) == 1) {
            return "双子座";
        } else if ("06-22".compareTo(tmp) == -1 && "07-22".compareTo(tmp) == 1) {
            return "巨蟹座";
        } else if ("07-23".compareTo(tmp) == -1 && "08-23".compareTo(tmp) == 1) {
            return "狮子座";
        } else if ("08-24".compareTo(tmp) == -1 && "09-23".compareTo(tmp) == 1) {
            return "处女座";
        } else if ("09-24".compareTo(tmp) == -1 && "10-23".compareTo(tmp) == 1) {
            return "天秤座";
        } else if ("10-24".compareTo(tmp) == -1 && "11-22".compareTo(tmp) == 1) {
            return "天蝎座";
        } else if ("11-23".compareTo(tmp) == -1 && "12-21".compareTo(tmp) == 1) {
            return "射手座";
        } else if ("12-22".compareTo(tmp) == -1 && "01-20".compareTo(tmp) == 1) {
            return "魔羯座";
        } else if ("01-21".compareTo(tmp) == -1 && "02-19".compareTo(tmp) == 1) {
            return "水瓶座";
        } else if ("02-20".compareTo(tmp) == -1 && "03-20".compareTo(tmp) == 1) {
            return "双鱼座";
        } else {
            return "";
        }
    }

    //1：普通会员，2：高级会员，3：VIP会员， 4：至尊VIP会员
    public static String getGridName(Long grid) {
        if (grid == null) return null;
        if (grid == 2) {
            return "高级会员";
        } else if (grid == 3) {
            return "VIP会员";
        } else if (grid == 4) {
            return "至尊VIP会员";
        }
        return "普通会员";
    }

    public static int getAge(Timestamp birthday) {
        if (birthday == null) return 0;
        Calendar tmp = Calendar.getInstance();
        tmp.setTime(new Date(birthday.getTime()));
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.YEAR) - tmp.get(Calendar.YEAR) + 1;
    }

    public static String getSex(String sex) {
        if (sex == null || sex.length() <= 0) return "";
        if ("m".equals(sex)) {
            return "男";
        } else if ("f".equals(sex)) {
            return "女";
        }
        return "";
    }

    public static String getUserType(String type) {
        if (type == null || type.length() <= 0) return "";
        if ("B".equals(type)) {
            return "商家";
        } else if ("C".equals(type)) {
            return "客户";
        }
        return "";
    }

    public static String getUserStatus(String status) {
        if (status == null || status.length() <= 0) return "";
        //normal(正常),inactive(未激活),delete(删除),reeze(冻结),supervise(监管)
        if ("normal".equals(status)) {
            return "正常";
        } else if ("inactive".equals(status)) {
            return "未激活";
        } else if ("delete".equals(status)) {
            return "删除";
        } else if ("reeze".equals(status)) {
            return "冻结";
        } else if ("supervise".equals(status)) {
            return "监管";
        }
        return "";
    }


    public static String getCountry(String state) {
        return "中国";
    }

    public static String getOrderStatus(String status) {
        if (status == null || status.length() <= 0) return "";
        if ("TRADE_NO_CREATE_PAY".equals(status)) {
            return "没有创建支付宝交易";
        } else if ("WAIT_BUYER_PAY".equals(status)) {
            return "等待买家付款";
        } else if ("WAIT_SELLER_SEND_GOODS".equals(status)) {
            return "等待卖家发货";
        } else if ("WAIT_BUYER_CONFIRM_GOODS".equals(status)) {
            return "等待买家确认收货";
        } else if ("TRADE_BUYER_SIGNED".equals(status)) {
            return "买家已签收";
        } else if ("TRADE_FINISHED".equals(status)) {
            return "交易成功";
        } else if ("TRADE_CLOSED".equals(status)) {
            return "付款以后用户退款成功";
        } else if ("TRADE_CLOSED_BY_TAOBAO".equals(status)) {
            return "付款以前，卖家或买家主动关闭交易";
        }
        return "";
    }
    
    public static String getOrderStatusByName(String statusName) {
        if (statusName == null || "".equals(statusName)) return "";
        if ("没有创建支付宝交易".equals(statusName)) {
            return "TRADE_NO_CREATE_PAY";
        } else if ("等待买家付款".equals(statusName)) {
            return "WAIT_BUYER_PAY";
        } else if ("等待卖家发货".equals(statusName)) {
            return "WAIT_SELLER_SEND_GOODS";
        } else if ("卖家已发货，等待买家确认".equals(statusName)) {
            return "WAIT_BUYER_CONFIRM_GOODS";
        } else if ("买家已签收".equals(statusName)) {
            return "TRADE_BUYER_SIGNED";
        } else if ("交易成功".equals(statusName)) {
            return "TRADE_FINISHED";
        } else if ("付款以后用户退款成功".equals(statusName)) {
            return "TRADE_CLOSED";
        } else if ("付款以前，卖家或买家主动关闭交易".equals(statusName)) {
            return "TRADE_CLOSED_BY_TAOBAO";
        } else if ("交易关闭".equals(statusName)) {
            return "TRADE_CLOSED_BY_TAOBAO";
        }
        return "";
    }

    public static BigDecimal getSubTotal(BigDecimal price, Long num) {
        return new BigDecimal(price.doubleValue()*num);
    }

    public static BigDecimal getAdd(BigDecimal discount1,BigDecimal discount){
    	return new BigDecimal(discount1.doubleValue() + discount.doubleValue());
    }

    public static String getShippingType(String type) {
        if (type == null || type.length() <= 0) return "";
        //物流方式.可选值:free(卖家包邮),post(平邮),express(快递),ems(EMS)
        if ("free".equals(type)) {
            return "卖家包邮";
        } else if ("post".equals(type)) {
            return "平邮";
        } else if ("express".equals(type)) {
            return "快递";
        } else if ("ems".equals(type)) {
            return "EMS";
        }
        return "";
    }

    public static BigDecimal getAfterTotal(BigDecimal price, Long num, BigDecimal discountFee) {
        if (discountFee == null) discountFee = new BigDecimal(0);
        return new BigDecimal(getSubTotal(price,num).doubleValue()-discountFee.doubleValue());
    }


    public static String getContactId(String nickName, int sellerid) throws Exception {
        String sql = "select baihuiid from tbuser where nick = '" + nickName + "' and sellerid = " + sellerid;
        UserDAO userDAO = new UserDAO();
        Map map = userDAO.findObject(sql);
        return (String) map.get("baihuiid");
    }

    public static int getPercentage(Long good, Long total) {
        if (total == null || total.longValue() == 0) return 0;
        double value = (good.longValue() / total.longValue()) * 100;
        return new Double(value).intValue();
    }

    public static int getInteger(Long value) {
        if (value == null) return 0;
        return value.intValue();
    }

}
